unit Unit1;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.UITypes,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls,

  GLS.VectorGeometry,
  GLS.Scene,
  GLS.PersistentClasses,
  GLS.VectorTypes,
  GLS.Mesh,
  GLS.SceneViewer,
  GLS.File3DS,
  GLS.VectorFileObjects,
  GLS.Objects,
  GLS.Cadencer,
  GLS.VectorLists,
  GLS.Texture,
  GLS.Material,
  GLS.Coordinates,

  GLS.BaseClasses,
  GLS.LightMapGenerator;

type
  TForm1 = class(TForm)
    GLScene1: TGLScene;
    GLSceneViewer1: TGLSceneViewer;
    GLCamera1: TGLCamera;
    FF: TGLFreeForm;
    dummy: TGLDummyCube;
    GLCadencer1: TGLCadencer;
    Button1: TButton;
    mlib: TGLMaterialLibrary;
    ls: TGLLightSource;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure GLCadencer1Progress(Sender: TObject; const deltaTime, newTime: Double);
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  sf: single;
begin
  sf := 0.023;
  FF.LoadFromFile('ff.3ds');
  FF.Scale.Scale(sf);
  FF.BuildOctree();
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  GLSceneViewer1.Free;
end;

procedure TForm1.GLCadencer1Progress(Sender: TObject; const deltaTime, newTime: Double);
begin
  // dummy.roll(30*deltatime);
  dummy.turn(-30 * deltaTime);
  // dummy.pitch(20*deltatime);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  WonderMaker: TGLLightMapGenerator;
  i: integer;
begin
  ShowMessage('Press OK and wait. Light Map generation takes 1-2 minutes');
  Button1.Enabled := false;
  GLCadencer1.Enabled := false;

  WonderMaker := TGLLightMapGenerator.Create;
  randomize;
  WonderMaker.AddOmni(AffineVectorMake(random(10) - 5, random(5) + 2, random(10) - 5), 20,
    155, 0, 0);
  WonderMaker.AddOmni(AffineVectorMake(random(10) - 5, random(5) + 2, random(10) - 5), 20,
    0, 155, 0);
  WonderMaker.AddOmni(AffineVectorMake(random(10) - 5, random(5) + 2, random(10) - 5), 20,
    0, 0, 155);

  WonderMaker.AddResiver(FF);
  WonderMaker.AddOccluder(FF);
  WonderMaker.EnvironmentLight := RGB(0, 0, 0);
  WonderMaker.LightMapsToFiles(512, 512, ExtractFilePath(Paramstr(0)));

  FF.MaterialLibrary := mlib;
  for i := 0 to FF.MeshObjects.Count - 1 do
  begin
    with mlib.AddTextureMaterial(inttostr(i), 'res_0_mesh_' + inttostr(i) + '.bmp') do
    begin
      Material.Texture.Enabled := true;
      Material.MaterialOptions := [moNoLighting];
    end;
    FF.MeshObjects.Items[i].FaceGroups[0].MaterialName := inttostr(i);
  end;

  FF.StructureChanged;

  caption := inttostr(FF.MeshObjects.TriangleCount);
  GLCadencer1.Enabled := true;
  WonderMaker.Destroy;
end;

end.
